/***************************************************************************
    Copyright 2008 Bastiaan Grutters
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published 
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;

public class Translations {
	/*
	 * Configuration variables
	 * 
	 * The language table is currently not used, so this is only for future reference
	 * 
	 * private static String LANGUAGE_TABLE = "language";
	 */
	/*
	 * The name of the translations table can be specified here
	 */
	private static String TRANSLATIONS_TABLE = "translations";
	/*
	 * You can specify the default language here
	 */
	private static String DEFAULT_LANGUAGE = "en";

	private java.sql.PreparedStatement preparedStatement;
	private java.sql.ResultSet resultSet;
	private Connection connection;
	private String location;
	
	public Translations( Connection connection ) {
		this.connection = connection;
		location = "";
	}
	
	public String getLocation() {
		return location;
	}

	public void setLocation( String location ) {
		this.location = location;
	}

	public String translate( String text, String language ) throws NoSuchAlgorithmException, SQLException {
		if( language == DEFAULT_LANGUAGE ) {
			return text;
		}
		else {
			MessageDigest messageDigest = MessageDigest.getInstance( "MD5" );
			byte[] md5Hash = messageDigest.digest( text.getBytes() );
			StringBuffer hexHash = new StringBuffer();
			String hexPaste = "";
			
			for( int i = 0; i < md5Hash.length; i ++ ) {
				hexPaste = Integer.toHexString( 0xFF & md5Hash[i] );
				while( hexPaste.length() < 2 ) {
					hexPaste = "0" + hexPaste;
				}
				hexHash.append( hexPaste );
			}
			
			String hash = hexHash.toString();
			
			preparedStatement = connection.prepareStatement( "SELECT translation, translated, approved " +
					"FROM " + TRANSLATIONS_TABLE + " " +
					"WHERE hash = ? AND language = ?" );
			preparedStatement.setString( 1, hash );
			preparedStatement.setString( 2, language );
			resultSet = preparedStatement.executeQuery();
			
			if( !resultSet.next() ) {
				preparedStatement = connection.prepareStatement( "INSERT INTO " + TRANSLATIONS_TABLE + " " +
						"( text, language, hash, url ) " +
						"VALUES( ?, ?, ?, ? )" );
				preparedStatement.setString( 1, text );
				preparedStatement.setString( 2, language );
				preparedStatement.setString( 3, hash );
				preparedStatement.setString( 4, location );
				preparedStatement.execute();
				return text;
			}
			else if( resultSet.getInt( 2 ) == 1 && resultSet.getInt( 3 ) == 1 ) {
				return resultSet.getString( 1 );
			}
			else {
				return text;
			}
		}
	}
}
